<template>
  <div class="question-container">
    <el-card>
      <!-- 筛选-->
      <el-form label-width="80px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input style="width: 200px" v-model="requestParams.keyword"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="12">
            <el-form-item style="text-align: right">
              <el-button @click="clear()">清除</el-button>
              <el-button @click="filter()" type="primary">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 数据记录 -->
      <el-alert
        style="margin-bottom: 15px"
        class="alert"
        type="info"
        show-icon
        :closable="false"
        :title="`数据一共 ${total} 条`"
      >
      </el-alert>

      <!-- 列表 -->
      <el-table :data="questions">
        <el-table-column label="编号" prop="id" width="220px"></el-table-column>
        <el-table-column label="题型" width="80px">
          <template slot-scope="scope">
            {{
              questionType.find(
                (item) => item.value === +scope.row.questionType
              ).label
            }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="220px">
          <template slot-scope="scope">
            <div v-for="item in scope.row.questionIDs" :key="item.number">
              <a href="#" @click.prevent="openPreviewDialog(item)" style="color: #09f">{{ item.number }}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" width="180px">
          <template slot-scope="scope">
            {{scope.row.addTime | parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column
          label="答题时间(s)"
          prop="totalSeconds"
        ></el-table-column>
        <el-table-column
          label="正确率(%)"
          prop="accuracyRate"
        ></el-table-column>
        <el-table-column label="录入人" prop="userName"></el-table-column>
        <el-table-column label="操作" width="80px">
          <template slot-scope="scope">
            <el-button
              plain
              type="danger"
              size="small"
              circle
              icon="el-icon-delete"
              @click="delQuestion(scope.row)"
              title="删除"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        :page-sizes="[5, 10, 20, 50]"
        layout=" prev, pager, next, sizes, jumper"
        @current-change="pager"
        @size-change="handleSizeChange"
      >
      </el-pagination>

      <!-- 预览 -->
      <questions-preview ref="questionPreview" :data="questionInfo"></questions-preview>
    </el-card>
  </div>
</template>

<script>
import { questionType } from '@/api/hmmm/constants.js'
import { randoms as questionList, removeRandoms as questionDel } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview1'
export default {
  components: {
    QuestionsPreview
  },
  data () {
    return {
      questionInfo: {},
      questionType,
      total: 0,
      // 注意：如果响应响应式数据 先声明
      questions: [],
      requestParams: {
        keyword: null,
        page: 1,
        pagesize: 20
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {

    openPreviewDialog (questionInfo) {
      // console.log(questionInfo)
      this.questionInfo = questionInfo
      this.$nextTick(() => {
        this.$refs.questionPreview.open()
      })
    },

    async delQuestion (question) {
      console.log(question)
      await this.$confirm('此操作将永久删除该题组,是否继续?', '温馨提示!', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })

      await questionDel({ id: question.id })
      this.$message.success('删除成功')
      this.getList()
    },
    clear () {
      this.requestParams.keyword = null
    },
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    handleSizeChange (size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.getList()
    },
    pager (np) {
      console.log(np)
      this.requestParams.page = np
      this.getList()
    },
    async getList () {
      // const res = await questionList(this.requestParams)
      // console.log(res)
      const {
        data: { items, counts }
      } = await questionList(this.requestParams)
      this.questions = items
      this.total = counts
    }
  }
}
</script>

<style lang="scss">
.question-container {
  padding: 0 10px;
  margin: 10px 0;
}
.btn_wrapper {
  margin-bottom: 15px;
}
</style>
